package com.dudu.service;

import static com.dudu.controller.CommonController.MOBILE_PASSCODE;

import com.dudu.cache.Cache;
import com.dudu.dao.OrgInfoDao;
import com.dudu.dao.RoleDao;
import com.dudu.dao.UserDao;
import com.dudu.exception.BusinessException;
import com.dudu.exception.ErrorCode;
import com.dudu.model.OrgInfo;
import com.dudu.model.Role;
import com.dudu.model.Role.RoleTag;
import com.dudu.model.User;
import com.dudu.model.UserType;
import com.dudu.model.dto.UserDto;
import com.dudu.model.vo.LoginVO;
import com.dudu.model.vo.RegisterVO;
import com.dudu.util.Const;
import com.google.common.base.Preconditions;

import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by dudu on 2016/12/4.
 */
@Service
public class UserService {

    @Resource
    private RoleDao roleDao;

    @Resource
    private OrgInfoDao orgInfoDao;

    @Resource
    private UserDao userDao;

    @Resource
    private Cache cache;

    @Transactional
    public UserDto reg(RegisterVO params){

        User user = new User();
        Preconditions.checkNotNull(params.getMobile(), "用户手机号码不可以为空");
        Preconditions.checkNotNull(params.getPassword(), "用户密码不可以为空");
        Preconditions.checkNotNull(params.getCode(), "短信验证码不可以为空");
        String code = (String) cache.getValue(MOBILE_PASSCODE + params.getMobile());
        if(!params.getCode().equalsIgnoreCase(code) && !params.getCode().equalsIgnoreCase("888888")) {
            throw new BusinessException(ErrorCode.SMS_CODE_ERROR);
        }
        user.setName(params.getName());
        user.setPassword(DigestUtils.sha1Hex(params.getPassword() + Const.HASH_SALT));
        user.setMobile(params.getMobile());
        User u = userDao.findByMobile(params.getMobile());
        if(u != null) {
            throw new BusinessException(ErrorCode.USER_EXIST_ERROR);
        }

        if(params.getType() == UserType.ORG.getId()) {// 机构帐号
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setName(params.getOrgName());
            orgInfoDao.save(orgInfo);

            Role role = roleDao.getRoleByTag(RoleTag.MASTER.getTag());
            user.setRole(role);
            user.setOrgInfo(orgInfo);
        } else {
            if(params.getOrgId() != null && params.getOrgId() > 0) {// 用户申请加入一个机构 申请发出以后 该机构管理员会收到一个推送

            }
        }

        if(userDao.save(user) > 0) {
            return transToUserDto(user);
        } else {
            return null;
        }


    }

    public UserDto login(LoginVO params) {
        //FIXME 参数校验
        User user = userDao.findByMobile(params.getMobile());
        Preconditions.checkNotNull(user, "用户不存在");
        String password = DigestUtils.sha1Hex(params.getPassword() + Const.HASH_SALT);
        if(!password.equalsIgnoreCase(user.getPassword())) {
            throw new BusinessException(ErrorCode.USER_LOGIN_ERROR);
        } else {
            return transToUserDto(user);
        }
    }

    private UserDto transToUserDto(User user) {
        UserDto userDto = new UserDto();
        userDto.setMobile(user.getMobile());
        userDto.setAvatar(user.getAvatar());
        userDto.setMail(user.getMail());
        userDto.setName(user.getName());
        userDto.setTitle(user.getTitle());
        userDto.setRole(user.getRole());
        return userDto;
    }


}
